package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import java.util.List;

import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.LinkBean;
import cn.jinjimi.app.main.adapter.pagerAdapter.AdvAdapter;
import cn.jinjimi.app.main.widget.RollViewPager.RollPagerView;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.framework.router.Router;

/**
 * @author zhenhua
 */

public class HomeHeadView extends LinearLayout {
    private RollPagerView rollPagerView;
    private AdvAdapter mAdvAdapter;

    public HomeHeadView(Context context) {
        super(context);
        LayoutInflater.from(getContext())
            .inflate(R.layout.view_home_head_view, this);
        initView();
    }

    private void initView() {
        initBanner();
    }

    private void initBanner() {
        rollPagerView = (RollPagerView) findViewById(R.id.roll_pager);
        rollPagerView.setAnimationDurtion(1000);
        mAdvAdapter = new AdvAdapter(getContext(), R.drawable.default_bg);
        mAdvAdapter.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = (String) v.getTag(R.id.url);
                if (StringUtils.isNotEmpty(url)) {
                    Router.create(getContext(), url).open();
                }
            }
        });
    }

    /**
     * 设置广告数据
     * @param ads
     */
    public void setAdvData(final List<LinkBean> ads) {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                if (ads != null && !ads.isEmpty()) {
                    rollPagerView.setVisibility(VISIBLE);
                    mAdvAdapter.setItems(ads);
                    rollPagerView.setAdapter(mAdvAdapter);
                } else {
                    rollPagerView.setVisibility(GONE);
                }
            }
        }, 500);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
